package winOrDie.map_editor;

import java.util.Vector;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Table;

import winOrDie.map_editor.Event_Map_Editor_Table.Event_Type_Map_Editor_Table;

public class Composite_Map_Editor_Table extends Composite {

	private Table table = null;

	
	/**
	 * This public interface is the Listener for {@link Composite_Map_Editor_Table} custom events
	 */
	public interface Listener_Map_Editor_Table extends Listener
	{
		public abstract void on_Event_Map_Editor_Table(Event_Map_Editor_Table eventMapEditorTable);
	}
	
	/**
	 * {@link Vector} of {@link Listener_Map_Editor_Table}
	 */
	Vector<Listener_Map_Editor_Table> vector_Listeners_Map_Editor_Table = new Vector<Listener_Map_Editor_Table>();
	
	/**
	 * This public synchronized method allows to add {@link Listener_Map_Editor_Table}
	 * @param listenerMapEditorTable
	 */
	public synchronized void add_Listener_Map_Editor_Table(Listener_Map_Editor_Table listenerMapEditorTable)
	{
		vector_Listeners_Map_Editor_Table.addElement(listenerMapEditorTable);
	}
	
	/**
	 * This public synchronized method allows to add {@link Listener_Map_Editor_Table}
	 * @param listenerMapEditorTable
	 */
	public synchronized void remove_Listener_Map_Editor_Table(Listener_Map_Editor_Table listenerMapEditorTable)
	{
		vector_Listeners_Map_Editor_Table.removeElement(listenerMapEditorTable);
	}
	
	/**
	 * This protected method notifies to all the {@link Event_Map_Editor_Table} passed as parameter to all listeners in 
	 * {@link Vector} of {@link Listener_Map_Editor_Table} 
	 * @param eventMapEditorTable
	 */
	protected void notify_Event_Map_Editor_Table(Event_Map_Editor_Table eventMapEditorTable)
	{
		for (Listener_Map_Editor_Table listenerMapEditorTable : vector_Listeners_Map_Editor_Table) {
			listenerMapEditorTable.on_Event_Map_Editor_Table(eventMapEditorTable);
		}
	}
	
	
	
	
	
	
	
	public Composite_Map_Editor_Table(Composite parent, int style) {
		super(parent, style);
		initialize();
	}


	
	
	
	private void initialize() {

		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		this.setLayout(gridLayout);
		//create_Table();
	}

	private void create_Table()
	{
		GridData gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = SWT.FILL;
		gridData.verticalAlignment = SWT.FILL;
		
		table = new Table(this, 
				SWT.NONE |
				SWT.V_SCROLL |
				SWT.H_SCROLL
				);
		table.setHeaderVisible(false);
		table.setLinesVisible(true);
		table.setLayoutData(gridData);
		
		
		
/*		int columns = 20;
		
		for (int i = 0; i < columns; i++) {
			new TableColumn(table, SWT.NONE);
		}*/
		
		
	
		/*
		 * Code to use TableEditor object
		 */
/*		int items = 40;
		for (int i = 0; i < items; i++) {
			
			TableItem tableItem = new TableItem(table, SWT.NONE);
			//tableItem.setText("eo");
			tableItem.setText(new String[] {" "," "});
			//tableItem.setBackground(new Color(getDisplay(), new RGB(red, green, blue)));
			
			
			label_test = new Label(table, SWT.NONE);
			label_test.setText("Label");
			label_test.setBackground(color_blue);
			
			TableEditor tableEditor = new TableEditor(table);
			tableEditor.grabHorizontal = tableEditor.grabVertical = true;
			tableEditor.setEditor(label_test, tableItem, 1);
		}*/
		
		
		
/*		int items = 40;
		for (int i = 0; i < items; i++) {
			
			TableItem tableItem = new TableItem(table, SWT.NONE);
			//tableItem.setText("eo");
			//tableItem.setText(new String[] {" "," "});
			//tableItem.setBackground(new Color(getDisplay(), new RGB(red, green, blue)));
			tableItem.setBackground(1, color_blue);

		}*/
		
/*		for (TableColumn tableColumn : table.getColumns()) {
			tableColumn.pack();
		}*/
		

		// to avoid to select rows, which is the default behaviour (Tom Schindl in news.eclipse.platform.swt)		
		table.addListener(SWT.EraseItem, new Listener()
		{
			public void handleEvent(Event event)
			{
				if((event.detail & SWT.SELECTED) != 0 ){
		            event.detail &= ~SWT.SELECTED;
		        }
			}			
		});
		
		
		/*
		 * Source code modified from...
		 * 
		 * Table example snippet: find a table cell from mouse down (works for any table style)
		 *
		 * For a list of all SWT example snippets see
		 * http://www.eclipse.org/swt/snippets/
		 * 
		 * Snippet110 
		 */
		
		final Widget source_Widget = this;
		table.addListener (SWT.MouseDown, new Listener () {
			public void handleEvent (Event event) {
				Rectangle clientArea = table.getClientArea ();
				Point pt = new Point (event.x, event.y);
				int index = table.getTopIndex();
				while (index < table.getItemCount()) {
					boolean visible = false;
					TableItem item = table.getItem (index);
					for (int i=0; i < table.getColumnCount(); i++) {
						Rectangle rect = item.getBounds (i);
						if (rect.contains (pt)) {
							//System.out.println ("Item " + index + "-" + i);
							
	
							
							/*
							 * Code for notify Cell Clicked to listeners
							 */
							Point point_Cell_Selected = new Point(i, index);
							Event_Map_Editor_Table eventMapEditorTable = new Event_Map_Editor_Table(source_Widget);
							eventMapEditorTable.setEvent_Type(Event_Type_Map_Editor_Table.Cell_Clicked);
							eventMapEditorTable.setPoint_Cell_Selected(point_Cell_Selected);
							notify_Event_Map_Editor_Table(eventMapEditorTable);
							
						}
						if (!visible && rect.intersects (clientArea)) {
							visible = true;
						}
					}
					if (!visible) return;
					index++;
				}
			}
		});
		
		
		
		/*
		 * Resize the row height using a MeasureItem listener
		 * http://lekkimworld.com/2008/03/27/setting_table_row_height_in_swt.html
		 */
/*		table.addListener(SWT.MeasureItem, new Listener() {
		   public void handleEvent(Event event) {
		      // height cannot be per row so simply set
		      event.height = Matrix_Cell_Height;
		   }
		});*/
		
	}
	
	
	
	
	private int Matrix_Cell_Width = 20;
	private int Matrix_Cell_Height = 20;
	
	
	
	
	/**
	 * This public method returns cells width
	 * @return int
	 */
	public int getMatrix_Cell_Width() {
		return Matrix_Cell_Width;
	}

	public void setMatrix_Cell_Width(int matrixCellWidth) {
		Matrix_Cell_Width = matrixCellWidth;
		
		for (TableColumn tableColumn : table.getColumns()) {
			tableColumn.setWidth(Matrix_Cell_Width);
		}
	}

	public int getMatrix_Cell_Height() {
		return Matrix_Cell_Height;
	}

	public void setMatrix_Cell_Height(int matrixCellHeight) {
		Matrix_Cell_Height = matrixCellHeight;
	}

	public void set_Matrix_Size(int columns, int rows)
	{
		/*
		 * Clear items and columns
		 */
		//table.clearAll();
		create_Table();
		
//		for (int column = 0; column < table.getColumnCount(); column++) {
//			table.getColumn(column).dispose();
//		}
//		
		
		for (int i = 0; i < columns; i++) {
			TableColumn column = new TableColumn(table, SWT.NONE, i);
			column.setWidth(Matrix_Cell_Width);
		}
		
		
		
		String Item_content[] = new String[rows];
//		for (String string : Item_content) {
//			string = " ";
//		}
		
		
		for (int row = 0; row < rows; row++) {
			TableItem tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(Item_content);
		}
		
		
		//table.pack();
		pack();
	}
	
	
	/**
	 * This public method allows to set a Cell background color
	 * @param column
	 * @param row
	 * @param rgb_color
	 */
	public void set_Cell_Color(int column, int row, RGB rgb_color)
	{
		Color color = new Color(Display.getCurrent(), rgb_color);
		
		TableItem tableItem = table.getItem(row);
		tableItem.setBackground(column, color);
		
		
		if (column == 0)
		{
			table.deselectAll();
//			Display.getCurrent().readAndDispatch();
//			Display.getCurrent().update();
			
		}
	
		
		
		
	}
	
	/**
	 * This public method allows to set {@link Image} on Cell
	 * @param column
	 * @param row
	 * @param image
	 */
	public void set_Cell_Image(int column, int row, Image image)
	{
		TableItem tableItem = table.getItem(row);
		tableItem.setImage(column, image);
	}
	
	
	
}
